package Dao;

import entity.UserBean;
import util.JDBCUtil;

import java.sql.SQLException;
import java.util.List;

public class UserDao {

    private static UserDao userDao;

    //查询所有
    public List<UserBean> all() {
        List<UserBean> list = null;
        String sql = "select UiD,Uname,Email,Uphone,Ustate,Rname from userList,roles where userList.UroleId=roles.RId ORDER BY UiD";
        list = JDBCUtil.list(sql, UserBean.class);
        return list;
    }
//登录
    public UserBean login(String userName, String pwd){
        UserBean userBean = null;
        String sql = "select UiD,Uname,Email,Uphone,Ustate,Rname from userList,roles  where Uname = ? and Upassword = ?";
        userBean = JDBCUtil.one(sql,UserBean.class,userName,pwd);
        return userBean;
    }

    //根据id获取用户
    public UserBean one(int id) {
        UserBean userBean = null;
        String sql = "select UiD,Uname,Email,Uphone,Ustate,Rname,UcreateDate from userList,roles where userList.UroleId=roles.RId AND Uid=?";
        userBean = JDBCUtil.one(sql, UserBean.class, id);
        return userBean;
    }
    //根据id获取个人信息
    public UserBean personalInformation(int id) {
        UserBean userBean = null;
        String sql = "SELECT Uid,Uname,Upassword,Email,Uphone,Rname FROM roles,userList WHERE UroleId = Rid AND Uid=? ORDER BY Rid";
        userBean = JDBCUtil.one(sql, UserBean.class, id);
        return userBean;
    }

    //添加新用户
    public int add(UserBean userBean) {
        int num = 0;
        String sql = "insert into userList (Uname,Email,Uphone,Upassword,UcreateDate) values (?,?,?,?,?)";
        num = JDBCUtil.update(sql, userBean.getUname(),userBean.getEmail(),userBean.getUphone(),userBean.getUpassword(),userBean.getUcreateDate());
        return num;
    }

    //根据id删除用户
    public int del(int id) {
        int num = 0;
        String sql = "delete from userList where Uid = ?";
        num = JDBCUtil.update(sql, id);
        return num;
    }

    //根据name搜索用户
    public List<UserBean> searchbyname(String searchName){
        List<UserBean> list = null;
        String sql = "select UiD,Uname,Email,Uphone,Ustate,Rname from userList,roles where userList.UroleId=roles.RId AND Uname like '%"+searchName+"%' ORDER BY UiD";
        list = JDBCUtil.list(sql, UserBean.class);
        return list;
    }

    //根据id改变状态
    public int changeState(int id,boolean uState){
        int num = 0;
        String sql = "UPDATE userList SET Ustate=? WHERE Uid=?";
        num = JDBCUtil.update(sql,uState,id);
        return num;
    }

    //更新用户信息
    public int update(UserBean userBean) {
        int num = 0;
        String sql = "UPDATE userList SET Email=?,Uphone=? WHERE Uid=?";
        num = JDBCUtil.update(sql, userBean.getEmail(),userBean.getUphone(), userBean.getUid());
        return num;
    }

    //修改个人信息
    public int updatePersonalInformation(UserBean userBean) {
        int num = 0;
        String sql = "UPDATE userlist SET Uname=?,Upassword=?,Email=?,Uphone=? WHERE Uid=?";
        num = JDBCUtil.update(sql, userBean.getUname(),userBean.getUpassword(), userBean.getEmail(),userBean.getUphone(),userBean.getUid());
        return num;
    }

    // 修改用户的角色id
    public int updateUsersRoleId(int uid,int radioRolesCheckedId){
        int num = 0;
        String sql = "UPDATE userList SET UroleId=? WHERE Uid=?";
        num = JDBCUtil.update(sql, radioRolesCheckedId,uid);
        return num;
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        UserBean userBean = new UserDao().personalInformation(1001);
//        System.out.println(userBean);
    }
}
